//
//  SYJEmoticonKeyboardView.swift
//  weibo
//
//  Created by 孙英建 on 16/4/24.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class SYJEmoticonKeyboardView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置UI控件
    func setUpUI(){
        
        // 添加控件
        addSubview(emoticonToolBar)
        addSubview(emoticonPageView)
        addSubview(pageControl)
        
        // 添加约束
        emoticonToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        
        emoticonPageView.snp_makeConstraints { (make) -> Void in
            make.left.trailing.top.equalTo(self)
            make.bottom.equalTo(emoticonToolBar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonPageView)
        }
        
        
        // 传递按钮的点击闭包
        emoticonToolBar.EmoticonToolBarButtonClosure = {[weak self] (tag: Int) in
           
            let indexPath = NSIndexPath(forItem: 0, inSection: tag)
            self?.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            // 设置PageControl的总数和当前选择页面
            self!.pageControl.numberOfPages = SYJEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
            
            self!.pageControl.currentPage = 0
            
        }
        
    }

    
    // MARK: - 懒加载控件
    // 表情底部按钮
    private lazy var emoticonToolBar: SYJEmoticonToolBar = SYJEmoticonToolBar()
    
    // 显示表情的collectionview
    private lazy var emoticonPageView: SYJEmoticonPageView = {
       let emoticonV = SYJEmoticonPageView()
        emoticonV.bounces = false
        emoticonV.pagingEnabled = true
        emoticonV.showsVerticalScrollIndicator = false
        emoticonV.showsHorizontalScrollIndicator = false
        emoticonV.delegate = self
        return emoticonV
    }()
    
    // 显示页码
    private lazy var pageControl: UIPageControl = {
        let pageC = UIPageControl()
        pageC.numberOfPages = SYJEmoticonTools.sharedTools.allEmoticons[0].count
        pageC.currentPage = 0
        
        // 通过kvc为其赋值
        pageC.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        pageC.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        
        return pageC
    }()
    
    
}

// MARK: - 代理方法
extension SYJEmoticonKeyboardView: UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 中心点
        let centerX = kWidth * 0.5 + scrollView.contentOffset.x
        
        let cellArr = emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        // 如果屏幕中不是两个cell则不判断
        if cellArr.count != 2{
            return
        }
        
        var indexPath: NSIndexPath?
        
        // 获取屏幕中显示的两个cell
        let firstCell = cellArr.first!
        let lastCell = cellArr.last!
        
        // 判断中心点包含在哪个cell中
        if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
            indexPath = emoticonPageView.indexPathForCell(firstCell)
        }else{
            indexPath = emoticonPageView.indexPathForCell(lastCell)
        }
        
        emoticonToolBar.setEmoticonCurrentButton(indexPath!.section)
        
        // 共有多少页码
        pageControl.numberOfPages = SYJEmoticonTools.sharedTools.allEmoticons[indexPath!.section].count
        
        // 当前选中页码
        pageControl.currentPage = indexPath!.item
        
    }
    
}














